var http = require('http'),
    url  = require('url');

// Custom module
var config = require('./config').config,
    router = require('./router');

var encoding = config['encoding'];
var port = config['port'];

function start() {
  function onRequest(req, res) {
    var path = url.parse(req.url).pathname;
    var postData = "";
    
    req.setEncoding(encoding);
    req.addListener("data", function(postDataChunk) {
      postData += postDataChunk;
      //console.log('Recevied POST data chunk ' + postDataChunk);
    });
    req.addListener("end", function() {
      router.route(req, res, path, postData);
    });
  }

  http.createServer(onRequest).listen(port);
  console.log('Server running at http://127.0.0.1:' + port + '/');
  // log format
  // source | date-time | request
  console.log('                               dateTime │              request');
  console.log('────────────────────────────────────────┼───────────────────────');
}

exports.start = start;